from app.outdoor.models import Activity, ActivityType
from app.account.models import UserInfo
from common.dictInfo import model_to_dict
from common.authCheck.userAuth import check_login, getUser
from django.http import JsonResponse
from rest_framework.views import APIView


class ActivityInfoView(APIView):

    @check_login
    def get(self, request, aid):
        """
        获取活动信息详情
        :param request:
        :param aid:
        :return:
        """
        user = getUser(request.session.get('login'))
        activity = Activity.objects.filter(id=aid)
        if not activity.exists():
            return JsonResponse({
                'status': False,
                'errMsg': '活动不存在'
            }, status=404)
        activity = activity[0]
        if activity.sponsor != user or user not in activity.leader.all():
            # 既不是发起者，也不是领队人，也不是参与者的话，不允许查看未发布的活动
            if activity.status == '1':  # 活动未发布
                return JsonResponse({
                    'status': False,
                    'errMsg': '活动不存在'
                }, status=404)
        activityDict = model_to_dict(activity)
        return JsonResponse({
            'status': True,
            'activity': activityDict
        })

    # @check_login
    # def post(self, request):
    #     """
    #     认证领队发布新的户外活动
    #     :param request:
    #     :return:
    #     """
    #     user = getUser(request.session.get('login'))
    #     if user.user_role != '99':
    #         return JsonResponse({
    #             'status': False,
    #             'errMsg': '你没有权限操作'
    #         }, status=401)
    #     jsonParams = json.loads(request.body.decode('utf-8'))
    #     if jsonParams.get('name') is None:
    #         return JsonResponse({
    #             'status': False
    #         })
        # activity_type = ActivityType.objects.filter(name=jsonParams.get('type_name', ''))
        # if not activity_type.exists():
        #     activity_type = ActivityType.objects.get(name='其他')
        # else:
        #     activity_type = activity_type[0]
        # from app.outdoor.models import ActivityTypeTemplate
        # templates = ActivityTypeTemplate.objects.filter(related_activity_type=activity_type)
        # if templates.exists():
        #
